package com.epam.rentcar.utils;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public abstract class GenericSecurityFilter implements Filter{
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		if (isValidRequest(request)){           
			chain.doFilter(request, response);
		}else { 
			HttpServletResponse httpServletResponse  = (HttpServletResponse) response; 
			httpServletResponse.sendError(401, "You are not authorized to view this page");
		}
	}

	public void destroy() {
	}

	abstract boolean isValidRequest(ServletRequest request);
}
